package com.shuhua.common.exception;

import com.shuhua.common.result.ResultCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 断言工具类，用于简化业务异常的抛出
 */
public class Assert {
    
    /**
     * 断言表达式为true，否则抛出BusinessException
     */
    public static void isTrue(boolean expression, ResultCode resultCode) {
        if (!expression) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言表达式为true，否则抛出BusinessException
     */
    public static void isTrue(boolean expression, ResultCode resultCode, String message) {
        if (!expression) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言表达式为false，否则抛出BusinessException
     */
    public static void isFalse(boolean expression, ResultCode resultCode) {
        if (expression) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言表达式为false，否则抛出BusinessException
     */
    public static void isFalse(boolean expression, ResultCode resultCode, String message) {
        if (expression) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言对象为null，否则抛出BusinessException
     */
    public static void isNull(Object object, ResultCode resultCode) {
        if (object != null) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言对象为null，否则抛出BusinessException
     */
    public static void isNull(Object object, ResultCode resultCode, String message) {
        if (object != null) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言对象不为null，否则抛出BusinessException
     */
    public static void notNull(Object object, ResultCode resultCode) {
        if (object == null) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言对象不为null，否则抛出BusinessException
     */
    public static void notNull(Object object, ResultCode resultCode, String message) {
        if (object == null) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言字符串不为空，否则抛出BusinessException
     */
    public static void notEmpty(String str, ResultCode resultCode) {
        if (StringUtils.isEmpty(str)) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言字符串不为空，否则抛出BusinessException
     */
    public static void notEmpty(String str, ResultCode resultCode, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言字符串不为空白，否则抛出BusinessException
     */
    public static void notBlank(String str, ResultCode resultCode) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言字符串不为空白，否则抛出BusinessException
     */
    public static void notBlank(String str, ResultCode resultCode, String message) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言集合不为空，否则抛出BusinessException
     */
    public static <T> void notEmpty(Collection<T> collection, ResultCode resultCode) {
        if (collection == null || collection.isEmpty()) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言集合不为空，否则抛出BusinessException
     */
    public static <T> void notEmpty(Collection<T> collection, ResultCode resultCode, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言Map不为空，否则抛出BusinessException
     */
    public static <K, V> void notEmpty(Map<K, V> map, ResultCode resultCode) {
        if (map == null || map.isEmpty()) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言Map不为空，否则抛出BusinessException
     */
    public static <K, V> void notEmpty(Map<K, V> map, ResultCode resultCode, String message) {
        if (map == null || map.isEmpty()) {
            throw new BusinessException(resultCode, message);
        }
    }
    
    /**
     * 断言对象equals，否则抛出BusinessException
     */
    public static void equals(Object o1, Object o2, ResultCode resultCode) {
        if (!Objects.equals(o1, o2)) {
            throw new BusinessException(resultCode);
        }
    }
    
    /**
     * 断言对象equals，否则抛出BusinessException
     */
    public static void equals(Object o1, Object o2, ResultCode resultCode, String message) {
        if (!Objects.equals(o1, o2)) {
            throw new BusinessException(resultCode, message);
        }
    }
} 